use strict;
use warnings;

use eve_const;
use api_utils;
use api_loader;
use api_calls;
use output_tables;

my $charid  = '';
my $name    = '';
my $profiles;
my $apidata;

#########################################################################
##### SELECT CHARACTER
##### INPUTS:   NONE
##### OUTPUTS:  character ID, character name
#########################################################################
sub select_char {
	logger("BEGIN select_char");
	print "Please select your character:\n";
	my $chars = [];
	foreach my $i (keys(%{$apidata->{$profiles->{ACTIVE_PROFILE}}{account}{characters}{characters}})) {
		push @$chars, $i;
		print ' ['.@$chars.'] '.$i.' in corporation '.$apidata->{$profiles->{ACTIVE_PROFILE}}{account}{characters}{characters}{$i}{corporationname}."\n";
	}
	fail("mgr.pl", "select_char", "You have no characters?") unless @$chars;
	print "> ";
	my $input = <>; chomp $input;
	logger("END select_char");
	return ($apidata->{$profiles->{ACTIVE_PROFILE}}{account}{characters}{characters}{$chars->[$input-1]}{characterid}, $chars->[$input-1]); 
}

#########################################################################
##### CHARACTER DATA MENU
##### INPUTS:  NONE
##### OUTPUTS:  NONE
#########################################################################
sub char_menu {
	logger("BEGIN char_menu");
	my $done = 0;
	while (not $done) {
		print "\n\n";
		print "CHARACTER MENU\n\n";
		print "CURRENT CHARACTER:  $name\n";
		print "\t\t1  Change character\n";
		print "\t\t2  View account balance\n";
		print "\t\t3  Get wallet journal\n";
		print "\t\t4  Get character sheet\n";
		print "\t\tx  Main menu\n";
		print "\n";
		print ">  ";
		my $input = <>;
		chomp $input;
		$input = lc($input);
		if ($input eq '1') {
			($charid, $name) = select_char;
		} elsif ($input eq '2') {
			$apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{char}{accountbalance} = get_accountbalance($profiles, 'char', $charid);
			print_accountbalance($profiles, 'char', $charid, $apidata);
		} elsif ($input eq '3') {
			$apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{char}{walletjournal} = get_walletjournal($profiles, 'char', $charid, $apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{char}{accountbalance});
			print_walletjournal($profiles, 'char', $charid, $apidata);
		} elsif ($input eq '4') {
			$apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{char}{charactersheet} = get_char_charactersheet($profiles, $charid, );
		} elsif ($input eq 'x') {
			logger("END char_menu");
			return;
		} else {
			print "INVALID SELECTION:  $input\n";
		}
	}
}

#########################################################################
##### CORPORATION DATA MENU
##### INPUTS:  NONE
##### OUTPUTS:  NONE
#########################################################################
sub corp_menu {
	logger("BEGIN corp_menu");
	my $done = 0;
	while (not $done) {
		print "\n\n";
		print "CORPORATION MENU\n\n";
		print "CURRENT CHARACTER:  $name\n";
		print "\t\t1  View account balance\n";
		print "\t\t2  Get wallet journal\n";
		print "\t\tx  Main menu\n";
		print "\n";
		print ">  ";
		my $input = <>;
		chomp $input;
		$input = lc($input);
		if ($input eq '1') {
			$apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{corp}{accountbalance} = get_accountbalance($profiles, 'corp', $charid);
			print_accountbalance($profiles, 'corp', $charid, $apidata);
		} elsif ($input eq '2') {
			$apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{corp}{walletjournal} = get_walletjournal($profiles, 'corp', $charid, $apidata->{$profiles->{ACTIVE_PROFILE}}{$charid}{corp}{accountbalance});
			print_walletjournal($profiles, 'corp', $charid, $apidata);
		} elsif ($input eq 'x') {
			logger("END corp_menu");
			return;
		} else {
			print "INVALID SELECTION:  $input\n";
		}
	}
}

#########################################################################
##### SETUP MENU
##### INPUTS:  NONE
##### OUTPUTS:  NONE
#########################################################################
sub setup_menu {
	logger("BEGIN setup_menu");
	my $done = 0;
	while (not $done) {
		print "\n\n";
		print "SETUP MENU\n\n";
		print "\t\t1  Clear cache files\n";
		print "\t\t2  Delete log files\n";
		print "\t\t3  Delete dump files\n";
		print "\t\t4  Delete output files\n";
		print "\t\t5  Manage API profiles\n";
		print "\t\tx  Main menu\n";
		print "\n";
		print ">  ";
		my $input = <>;
		chomp $input;
		$input = lc($input);
		if ($input eq '1') {
			clear_cache;
		} elsif ($input eq '2') {
			clear_logs;
		} elsif ($input eq '3') {
			clear_dumps;
		} elsif ($input eq '4') {
			clear_outputs;
		} elsif ($input eq '5') {
			$profiles = manage_api_profiles;
		} elsif ($input eq 'x') {
			logger("END setup_menu");
			return;
		} else {
			print "INVALID SELECTION:  $input\n";
		}
	}
}

#########################################################################
##########     MAIN PROGRAM     #########################################
#########################################################################
init_logs;
logger("BEGIN EXECUTION");
print "\n\n\n";
$profiles = manage_api_profiles;
logger("PROFILE = $profiles->{ACTIVE_PROFILE}");
print "READING SERVER DATA - THIS MAY TAKE A FEW MINUTES...\n";
$apidata->{eve}{errorlist}       = get_eve_errorlist($profiles);
$apidata->{eve}{reftypes}        = get_eve_reftypes($profiles);
$apidata->{eve}{certificatetree} = get_eve_certificatetree($profiles);
$apidata->{eve}{skilltree}       = get_eve_skilltree($profiles);
$apidata->{$profiles->{ACTIVE_PROFILE}}{account}{characters} = get_account_characters($profiles);
print_account_characters($profiles, $apidata);
($charid, $name) = select_char;
logger("CHARID = $charid");
logger("NAME = $name");

my $done = 0;
logger("BEGIN MAIN LOOP");
print "\n\n\n";
while (not $done) {
	print "EVE ONLINE MANAGEMENT TOOL\n\n";
	print "CURRENT API PROFILE:  $profiles->{ACTIVE_PROFILE}\n";
	print "CURRENT CHARACTER:  $name\n";
	print "\t1  Server Status\n";
	print "\t2  Account status\n";
	print "\t3  Character data\n";
	print "\t4  Corporation data\n";
	print "\t5  Setup\n";
	print "\tX  Exit\n";
	print "\n";
	print ">  ";
	my $input = <>;
	chomp $input;
	$input    = lc($input);
	if ($input eq '1') {
		$apidata->{server}{serverstatus} = get_server_serverstatus($profiles);
		print "SERVER $profiles->{$profiles->{ACTIVE_PROFILE}}{root} STATUS\n";
		print "\tTIME:  $apidata->{server}{serverstatus}{currenttime}\n";
		if ($apidata->{server}{serverstatus}{serveropen} =~ /true/i) {
			print "SERVER ONLINE";
		} elsif ($apidata->{server}{serverstatus}{serveropen} =~ /false/i) {
			print "SERVER OFFLINE";
		} else {
			print "STATUS UNKNOWN:  $apidata->{server}{serverstatus}{serveropen}\n";
		}
		print "\t ONLINE PLAYERS:  $apidata->{server}{serverstatus}{onlineplayers}\n\n";
	} elsif ($input eq '2') {
		$apidata->{$profiles->{ACTIVE_PROFILE}}{account}{accountstatus} = get_account_accountstatus($profiles);
		print_account_status($profiles, $apidata);
	} elsif ($input eq '3') {		
		char_menu;
	} elsif ($input eq '4') {
		corp_menu;
	} elsif ($input eq '5') {
		setup_menu;
	} elsif ($input eq 'x') {
		print "Exiting\n\n";
		$done = 1;
	} else {
		print "Invalid input:  $input\n";
	}
}
data_dump("APIDATA", $apidata);
logger("END PROGRAM");
